package com.wlyuan.open.scanner.facade;

import cn.hutool.json.JSONUtil;
import com.wlyuan.open.OpenBaseSyncDTO;
import com.wlyuan.open.scanner.domain.message.SyncMessage;
import com.wlyuan.rabbit.MessagePublisher;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author lijing
 * @description
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class SyncFacade {
    private final MessagePublisher messagePublisher;
    private final static String EXCHANGE_NAME_SYNC = "exchange.topic.sync";
    private final static String ROUTING_KEY = "sync";
    private final static String WAYBILL_ROUTING_KEY = "combine.sync";
    private final static String WAYBILL_EXCHANGE_NAME_SYNC = "exchange.topic.combine.sync";

    /**
     * 同步
     *
     * @param source
     */
    public void sync(OpenBaseSyncDTO source) {
        if (source == null) {
            logger.error("同步数据不存在");
            return;
        }
        messagePublisher.publish(new SyncMessage(JSONUtil.toJsonStr(source), source.getClass()), EXCHANGE_NAME_SYNC, ROUTING_KEY);
    }

    /**
     * 同步
     *
     * @param source
     */
    public void syncRef(OpenBaseSyncDTO source) {
        if (source == null) {
            logger.error("同步数据不存在");
            return;
        }
        messagePublisher.publish(new SyncMessage(JSONUtil.toJsonStr(source), source.getClass()), WAYBILL_EXCHANGE_NAME_SYNC, WAYBILL_ROUTING_KEY);
    }

}
